package com.atguigu.yygh.hosp.controller.api;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.yygh.common.handler.YYGHException;
import com.atguigu.yygh.common.result.R;
import com.atguigu.yygh.common.result.Result;
import com.atguigu.yygh.common.utils.HttpRequestHelper;
import com.atguigu.yygh.hosp.service.DepartmentService;
import com.atguigu.yygh.hosp.service.HospitalService;
import com.atguigu.yygh.hosp.service.HospitalSetService;
import com.atguigu.yygh.model.hosp.Hospital;
import com.atguigu.yygh.vo.hosp.DepartmentVo;
import com.atguigu.yygh.vo.hosp.HospitalQueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @Author:hx
 * @DateTime:2022/11/7 16:18
 * @Description:
 */
@RestController
@RequestMapping("/api/hosp")
public class ApiHospitalController {

    @Autowired
    private HospitalSetService hospitalSetService;
    @Autowired
    private HospitalService hospitalService;
    @Autowired
    private DepartmentService departmentService;

    /**
     * @Description 添加医院信息
     * @param request:
     * @return: com.atguigu.yygh.common.result.Result
     */
    @PostMapping("/saveHospital")
    public Result saveHospital(HttpServletRequest request){
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());

        //验证signKey
        String sign = JSONObject.toJSONString(paramMap.get("sign"));
        String hoscode = JSONObject.toJSONString(paramMap.get("hoscode"));
        String signKey = hospitalSetService.getSignKeyByHoscode(hoscode);
        if (StringUtils.isEmpty(sign) || StringUtils.isEmpty(signKey) || !sign.equals(signKey)){
            throw new YYGHException(201,"signKey有误!");
        }

        //保存
        String logoData = JSONObject.toJSONString(paramMap.get("logoData"));
        paramMap.put("logoData",logoData.replaceAll(" ","+"));
        hospitalService.saveHospital(paramMap);
        return Result.ok();
    }

    /**
     * @Description 展示医院信息
     * @param paramMap:
     * @return: com.atguigu.yygh.common.result.Result
     */
    @PostMapping("/hospital/show")
    public Result show(@RequestParam Map<String,String> paramMap){
        String hoscode = paramMap.get("hoscode");
        Hospital hospital = hospitalService.show(hoscode);
        return Result.ok(hospital);
    }

    /**
     * @Description 模糊查询所有医院名字
     * @param hosname:
     * @return: com.atguigu.yygh.common.result.R
     */
    @GetMapping("/getByHosname/{hosname}")
    public R getByHosname(@PathVariable String hosname){
        List<Hospital> hosnameList = hospitalService.getByHosname(hosname);
        return R.ok().data("list",hosnameList);
    }

    @GetMapping("/getPageList/{pageNum}/{pageSize}")
    public R getPageList(@PathVariable Integer pageNum, @PathVariable Integer pageSize, HospitalQueryVo hospitalQueryVo){
        Page<Hospital> page = hospitalService.findPage(pageNum,pageSize,hospitalQueryVo);
        return R.ok().data("pages",page);
    }

    @GetMapping("/show/{hoscode}")
    public R show(@PathVariable String hoscode){
        Map<String,Object> map = hospitalService.showByHoscode(hoscode);
        return R.ok().data(map);
    }

    @GetMapping("/findDepartment/{hoscode}")
    public R findDepartmentByHoscode(@PathVariable String hoscode){
        List<DepartmentVo> list = departmentService.getDeptByHoscode(hoscode);
        return R.ok().data("list",list);
    }
}
